/**============================================================================
 Confidential Information - Limited distribution to authorized persons only.
 This software is protected as an unpublished work under the U.S. Copyright
 Act of 1976.

 Copyright (c) 2011, BluePhoenix Solutions, Inc. All rights reserved.
 ----------------------------------------------------------------------------
 file......: WordsManagerListAdapter.java
 Author....: Vyacheslav Kovalyov
 Created...: 12/29/11

 Description:

 ============================================================================*/
package com.bphx.android.activities.adapters;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bphx.android.IStorage;
import com.bphx.android.activities.EditWordActivity;
import com.bphx.android.activities.WordManagerActivity;
import com.bphx.android.db.Storage;
import com.bphx.android.words.Word;

import java.util.List;

import static android.content.Context.LAYOUT_INFLATER_SERVICE;
import static com.bphx.android.R.id.*;
import static com.bphx.android.R.layout.word_manager_list_item;
import static com.bphx.android.utils.Constants.EDIT_WORD_ACTIVITY;
import static com.bphx.android.utils.Constants.WORD_KEY;

/**
 * @author Vyacheslav Kovalyov
 * @version 1.0
 */
public class WordsManagerListAdapter extends BaseAdapter {

    public WordsManagerListAdapter(WordManagerActivity parent, List<Word> items) {
        this.parentActivity = parent;
        this.items = items;
    }

    @Override
    public View getView(int position, final View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) parentActivity.getSystemService(LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(word_manager_list_item, parent, false);

        final Word word = (Word) getItem(position);

        TextView text = (TextView) view.findViewById(wordManagerText);
        text.setText(word.getOriginalWord());

        ImageView editImage = (ImageView) view.findViewById(wordManagerEdit);
        ImageView infoImage = (ImageView) view.findViewById(wordManagerInfo);
        ImageView removeImage = (ImageView) view.findViewById(wordManagerDelete);

        editImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(parentActivity, EditWordActivity.class);
                intent.putExtra(WORD_KEY, word);

                parentActivity.startActivityForResult(intent, EDIT_WORD_ACTIVITY);
            }
        });

        infoImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                parentActivity.showDialog(word);
            }
        });

        removeImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                IStorage storage = new Storage(parentActivity);
                storage.removeWord(word);
                items.remove(word);
                WordsManagerListAdapter.this.notifyDataSetChanged();
            }
        });

        return view;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).hashCode();
    }

    private final WordManagerActivity parentActivity;
    private final List<Word> items;
}
